package com.huisu.netty.chatserver;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class ChatServer {

    public static void chatServerStart(int port) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            serverBootstrap
                    .group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChatInit());
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if (channelFuture.isSuccess()) {
                        System.out.println("    \t\t\tSunny  \n" +
                                "                 *       +          \n" +
                                "           '                  |     \n" +
                                "       ()    .-.,=\"``\"=.    - o -   \n" +
                                "             '=/_       \\     |     \n" +
                                "          *   |  '=._    |          \n" +
                                "               \\     `=./`,        '\n" +
                                "            .   '=.__.=' `='      * \n" +
                                "   +                         +      \n" +
                                "        O      *        '       .   \n" +
                                "\t\t ChatServer启动成功(V1.0)                       ");
                    }else {
                        System.out.println("ChatServer启动失败");
                    }
                }
            });
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            bossGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        chatServerStart(6898);
    }
}
